<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Gets Attributes</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="reflectionclass.export.html">« ReflectionClass::export</a></li>
      <li style="float: right;"><a href="reflectionclass.getconstant.html">ReflectionClass::getConstant »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.reflectionclass.html">ReflectionClass</a></li>
    <li>Gets Attributes</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="reflectionclass.getattributes" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">ReflectionClass::getAttributes</h1>
  <p class="verinfo">(PHP 8)</p><p class="refpurpose"><span class="refname">ReflectionClass::getAttributes</span> &mdash; <span class="dc-title">Gets Attributes</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-reflectionclass.getattributes-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>ReflectionClass::getAttributes</strong></span>(<span class="methodparam"><span class="type">?</span><span class="type"><span class="type">string</span><span class="type"></span></span> <code class="parameter">$name</code><span class="initializer"> = <strong><code>null</code></strong></span></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code><span class="initializer"> = 0</span></span>): <span class="type">array</span></div>

  <p class="para rdfs-comment">
   Returns all attributes declared on this class as an array of <span class="type"><a href="class.reflectionattribute.html" class="type ReflectionAttribute">ReflectionAttribute</a></span>.
  </p>

 </div>


 <div class="refsect1 parameters" id="refsect1-reflectionclass.getattributes-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">name</code></dt>

    <dd>

     <p class="para">
      Filter the results to include only <span class="classname"><a href="class.reflectionattribute.html" class="classname">ReflectionAttribute</a></span>
      instances for attributes matching this class name.
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">flags</code></dt>

    <dd>

     <p class="para">
      Flags for determining how to filter the results, if <code class="parameter">name</code>
      is provided.
     </p>
     <p class="para">
      Default is <code class="literal">0</code> which will only return results for attributes that
      are of the class <code class="parameter">name</code>.
     </p>
     <p class="para">
      The only other option available, is to use <strong><code>ReflectionAttribute::IS_INSTANCEOF</code></strong>,
      which will instead use <code class="literal">instanceof</code> for filtering.
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-reflectionclass.getattributes-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Array of attributes, as a <span class="classname"><a href="class.reflectionattribute.html" class="classname">ReflectionAttribute</a></span> object.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-reflectionclass.getattributes-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5150">
    <p><strong>示例 #1 Basic usage</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">#[Attribute]<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Fruit&nbsp;</span><span style="color: #007700">{<br />}<br /><br /></span><span style="color: #FF8000">#[Attribute]<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Red&nbsp;</span><span style="color: #007700">{<br />}<br /><br /></span><span style="color: #FF8000">#[Fruit]<br />#[Red]<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Apple&nbsp;</span><span style="color: #007700">{<br />}<br /><br /></span><span style="color: #0000BB">$class&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">ReflectionClass</span><span style="color: #007700">(</span><span style="color: #DD0000">'Apple'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$attributes&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$class</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getAttributes</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #0000BB">fn</span><span style="color: #007700">(</span><span style="color: #0000BB">$attribute</span><span style="color: #007700">)&nbsp;=&gt;&nbsp;</span><span style="color: #0000BB">$attribute</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getName</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">$attributes</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; Fruit
    [1] =&gt; Red
)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5151">
    <p><strong>示例 #2 Filtering results by class name</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">#[Attribute]<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Fruit&nbsp;</span><span style="color: #007700">{<br />}<br /><br /></span><span style="color: #FF8000">#[Attribute]<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Red&nbsp;</span><span style="color: #007700">{<br />}<br /><br /></span><span style="color: #FF8000">#[Fruit]<br />#[Red]<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Apple&nbsp;</span><span style="color: #007700">{<br />}<br /><br /></span><span style="color: #0000BB">$class&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">ReflectionClass</span><span style="color: #007700">(</span><span style="color: #DD0000">'Apple'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$attributes&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$class</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getAttributes</span><span style="color: #007700">(</span><span style="color: #DD0000">'Fruit'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #0000BB">fn</span><span style="color: #007700">(</span><span style="color: #0000BB">$attribute</span><span style="color: #007700">)&nbsp;=&gt;&nbsp;</span><span style="color: #0000BB">$attribute</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getName</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">$attributes</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; Fruit
)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5152">
    <p><strong>示例 #3 Filtering results by class name, with inheritance</strong></p>
    <div class="example-contents">
     <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">interface&nbsp;</span><span style="color: #0000BB">Color&nbsp;</span><span style="color: #007700">{<br />}<br /><br /></span><span style="color: #FF8000">#[Attribute]<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Fruit&nbsp;</span><span style="color: #007700">{<br />}<br /><br /></span><span style="color: #FF8000">#[Attribute]<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Red&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">Color&nbsp;</span><span style="color: #007700">{<br />}<br /><br /></span><span style="color: #FF8000">#[Fruit]<br />#[Red]<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Apple&nbsp;</span><span style="color: #007700">{<br />}<br /><br /></span><span style="color: #0000BB">$class&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">ReflectionClass</span><span style="color: #007700">(</span><span style="color: #DD0000">'Apple'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$attributes&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$class</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getAttributes</span><span style="color: #007700">(</span><span style="color: #0000BB">Color</span><span style="color: #007700">::class,&nbsp;</span><span style="color: #0000BB">ReflectionAttribute</span><span style="color: #007700">::</span><span style="color: #0000BB">IS_INSTANCEOF</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #0000BB">fn</span><span style="color: #007700">(</span><span style="color: #0000BB">$attribute</span><span style="color: #007700">)&nbsp;=&gt;&nbsp;</span><span style="color: #0000BB">$attribute</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getName</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">$attributes</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; Red
)
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-reflectionclass.getattributes-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="methodname"><a href="reflectionclassconstant.getattributes.html" class="methodname" rel="rdfs-seeAlso">ReflectionClassConstant::getAttributes()</a> - Gets Attributes</span></li>
    <li class="member"><span class="methodname"><a href="reflectionfunctionabstract.getattributes.html" class="methodname" rel="rdfs-seeAlso">ReflectionFunctionAbstract::getAttributes()</a> - Gets Attributes</span></li>
    <li class="member"><span class="methodname"><a href="reflectionparameter.getattributes.html" class="methodname" rel="rdfs-seeAlso">ReflectionParameter::getAttributes()</a> - Gets Attributes</span></li>
    <li class="member"><span class="methodname"><a href="reflectionproperty.getattributes.html" class="methodname" rel="rdfs-seeAlso">ReflectionProperty::getAttributes()</a> - Gets Attributes</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>